package main

import (
	"fmt"
	"time"

	"gitee.com/zhangycsz/glin"
	"gitee.com/zhangycsz/glin/middleware"
	"gitee.com/zhangycsz/glin/util"
)

func main() {

	g := glin.New()
	g.Use(middleware.Logger())

	g.Use(func(next glin.HandlerFunc) glin.HandlerFunc {
		return func(c glin.Context) error {
			return next(c)
		}
	})

	g.GET("/ver", func(c glin.Context) error {
		q := c.QueryParam("q")
		fmt.Println("q=", q)
		r := c.CaptchaVerify("cap", q)
		out := "q=" + q
		if r {
			fmt.Println("校验正常")
			out += "校验正常"
		} else {
			fmt.Println("校验失败")
			out += "校验失败"
		}
		return c.String(200, out)
	})
	//直接显示图片
	g.GET("/cap", func(c glin.Context) error {
		return c.Captcha("cap", 100, 40, 4, 0.75, 10)
	})
	g.GET("/index", func(c glin.Context) error {
		outStr := "/index/index 输出："
		data, err := c.GetAuth("zhangyc")

		if err != nil {
			fmt.Println("err=", err)
		} else {
			fmt.Println("data=", string(data))
			outStr += string(data) + "；输出"
		}
		return c.String(200, outStr)
	})
	g.GET("/login", func(c glin.Context) error {
		err := c.SetAuth("zhangyc", []byte("当前时间:"+time.Now().Format("2006-01-02 15:04:05")))
		if err != nil {
			fmt.Println("setsession err=", err)
		} else {
			fmt.Println("setsession ok")
		}
		return c.String(200, "/index/login")
	})
	certFile, keyFile, _ := util.GetTlsFile()
	err := g.StartTLS("0.0.0.0:8086", certFile, keyFile)
	if err != nil {
		fmt.Println("err=", err)
	} else {
		fmt.Println("ok")
	}
	//g.Start("0.0.0.0:8086")
}
